﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThisGrayWorldAI
{
    /// <summary>
    /// Provides client-side interface gate to This Gray World AI handling system
    /// </summary>
    public partial class ClientServerInterface
    {
        /// <summary>
        /// Container class with static properties of the gate
        /// </summary>
        private sealed class RuntimeProperties
        {
            public static AppDomain ServerDomain;
            public static int ServerDomainHashCode; // Just in case amirite
            public static Guid SessionGuid;
            public static string ServerAppDomainName;
            public static string CrossDomainSingletonName;
            public static string ServerAssemblyPath;

            public static string ConfigFilename = @"Client.Interface.Config.xml";
            public static XmlDocument ConfigFile;
            public static string ConfigServerAppDomainName = @"/ThisGrayWorldAI/ServerAppDomainName"; // Maybe make it a proper XPath some day aah no fukkit
            public static string ConfigCrossDomainSingletonNamePath = @"/ThisGrayWorldAI/CrossDomainSingletonName";
            public static string ConfigServerAssemblyPath = @"/ThisGrayWorldAI/ServerAssemblyPath";
        }

        private static string ExtractXmlNode(string NodeXPath)
        {
            try
            {
                XmlNode DesiredNode = ConfigFile.SelectNodes(NodeXPath)[0];
                return DesiredNode.InnerText();
            }
            catch (System.Exception e) { throw; }

        }

        private static void ReadXmlConfig()
        {
            try
            {
                ConfigFile = new XmlDocument();
                ConfigFile.Load(RuntimeProperties.ConfigFilename);

                RuntimeProperties.ServerAppDomainName = ExtractXmlNode(RuntimeProperties.ConfigServerAppDomainName);
                RuntimeProperties.CrossDomainSingletonName = ExtractXmlNode(RuntimeProperties.ConfigCrossDomainSingletonNamePath);
                RuntimeProperties.ServerAssemblyPath = ExtractXmlNode(RuntimeProperties.ConfigServerAssemblyPath);
            }
            catch (System.Exception e) { throw; }
        }
    }

    public sealed class CrossDomainSingleton : MarshalByRefObject
    {
        private static CrossDomainSingleton instance = null;

        public static CrossDomainSingleton Instance
        {
            get
            {
                return instance ??
                    (instance =
                        AppDomain.CurrentDomain.GetData(RuntimeProperties.CrossDomainSingletonName)
                            as CrossDomainSingleton);
            }
            set
            {
                throw new NotSupportedException("CrossDomainSingleton.Instance reference is immutable");
            }
        }
    }

    public interface ICharacterEntityTemplate
    {
        string InitialName, CurrentName;



    }

}
